package org.ffsky.jsplugin.plugin;


import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.ffsky.litepack.conf.AppConf;
import com.ffsky.litepack.logger.L;
import ohos.agp.components.webengine.JsCallback;
import ohos.agp.components.webengine.WebView;
import org.ffsky.jsplugin.JsPlugin;

/**
 * hybrid runtime appInfo
 */
public class RuntimeJsPlugin extends JsPlugin {

    private static int HybridVersion = 100;

    public static void setHybridVersion(int hybridVersion){
        RuntimeJsPlugin.HybridVersion = hybridVersion;
    }

    @Override
    public void bindJsBridge(WebView webView) {
        super.bindJsBridge(webView);
        webView.addJsCallback("runtime_getAppInfo", new JsCallback() {
            @Override
            public String onCallback(String s) {
                L.i(TAG,"onCallback->",s);
                JSONObject json = new JSONObject();
                try {
                    json.put("versionName", AppConf.vName);
                    json.put("versionCode", AppConf.vCode);
                    json.put("hybridVersion", HybridVersion);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return json.toString();
            }
        });
    }
}
